'use strict';

const Controller = require('egg').Controller;

class IdentController extends Controller {
    //获取
    async getIdent() {
        const { ctx } = this;
        const data = await this.app.mysql.select('ident');
        data.forEach((item, index) => {
            item.key = index + 1;
        })
        ctx.body = {
            code: 200,
            data,
        };
    }

    //搜索
    async searchIdent() {
        const { ctx } = this;
        const { type, phone } = ctx.query;
        const data = await this.app.mysql.query(`SELECT * FROM ident WHERE type Like "%${type}%" and phone Like "%${phone}%"`);
        data.forEach((item, index) => {
            item.key = index + 1;
        })
        ctx.body = {
            code: 200,
            data,
        };
    }

    //删除
    async deleteIdent() {
        const { ctx } = this;
        const { id } = ctx.query;
        const data = await this.app.mysql.delete('ident', {
            id: id,
        });
        if (data.affectedRows !== 1) {
            ctx.body = {
                code: 500,
                message: "删除失败"
            };
        }
        ctx.body = {
            code: 200,
            message: "删除成功"
        };
    }
}

module.exports = IdentController;
